package com.doctor.windflower_doctor.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.doctor.windflower_doctor.R;
import com.doctor.windflower_doctor.actionBeen.AuthCodeAction;
import com.doctor.windflower_doctor.http.callback.HttpRequestCallBack;
import com.doctor.windflower_doctor.http.parser.impl.JsonParser;
import com.doctor.windflower_doctor.http.request.HttpRequest;
import com.doctor.windflower_doctor.http.request.HttpRequestParams;
import com.doctor.windflower_doctor.http.response.HttpResponseInfo;
import com.doctor.windflower_doctor.util.Check;
import com.doctor.windflower_doctor.util.Constent;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

public class AccountAuthentication extends BaseActivity implements Constent{
	private ImageButton back_imageButton;
	private EditText phone_email_editText;
	private Button find_Button;
	
	@Override
	protected void getSaveData(Bundle savedInstanceState) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void processLogic() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void setListener() {
		// TODO Auto-generated method stub
		back_imageButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		
		phone_email_editText.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				if(s.length()==11){
					find_Button.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
							if (Check.isMobileNO(phone_email_editText.getText().toString().trim())) {
								
								sendAuthCode();
							} else {
								Toast.makeText(AccountAuthentication.this, "请注意手机号格式",
										Toast.LENGTH_SHORT).show();
								
							}
						}
					});
					find_Button.setBackgroundResource(R.drawable.button_up_down);
					
				}else{
					find_Button.setBackgroundResource(R.drawable.unclick_button);
					find_Button.setClickable(false);
				}
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				
			}
		});
	}

	@Override
	protected void findViews() {
		// TODO Auto-generated method stub
		back_imageButton = (ImageButton) findViewById(R.id.back_imageButton);
		phone_email_editText = (EditText) findViewById(R.id.find_editText);
		find_Button = (Button) findViewById(R.id.find_Button);

	}

	private void sendAuthCode() {

		HttpMethod requestMethod = HttpMethod.POST;
		HttpRequestCallBack<AuthCodeAction> requestCallBack = new HttpRequestCallBack<AuthCodeAction>(
				new JsonParser(), AuthCodeAction.class) {
			@Override
			protected void handleResult(
					HttpResponseInfo<AuthCodeAction> responseInfo) {
				if (responseInfo.success.success) {
					Toast.makeText(AccountAuthentication.this, "发送验证码才成功！！",
							Toast.LENGTH_SHORT).show();
					startActivity(new Intent(AccountAuthentication.this,
							FindPassWord.class).putExtra("Phonenumber", phone_email_editText.getText()
									.toString().trim()));
				} else {
					Toast.makeText(AccountAuthentication.this,
							responseInfo.success.message, Toast.LENGTH_SHORT)
							.show();
				}

			}

			@Override
			protected void handleError(HttpException e, String msg) {
				Toast.makeText(AccountAuthentication.this, msg, Toast.LENGTH_SHORT)
						.show();
			}

			@Override
			public void onStart() {
				super.onStart();

			}

		};
		HttpRequestParams params = new HttpRequestParams(requestMethod,
				URL+AUTHCODE_URL, requestCallBack);
		params.addBodyParameter("telephone", phone_email_editText.getText()
				.toString().trim());
		params.addBodyParameter("appsecret", "123123");
		HttpRequest.send(params);

	
	}

	@Override
	public int getResLayoutId() {
		// TODO Auto-generated method stub
		return R.layout.account_authentication;
	}

}
